<template>
  <!-- 发货单发货组件 -->
  <div v-loading="loading" element-loading-text="加载中">
    <el-form ref="deliverForm" :model="deliverForm" :rules="rules" label-width="100px">
      <el-form-item label="快递公司：" prop="enterpriseExpressId">
        <el-select v-model="deliverForm.enterpriseExpressId" placeholder="请选择" class="width-200">
          <el-option v-for="(express, index) in expressList" :key="index" :label="express.expressName" :value="express.enterpriseExpressId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="快递单号：" prop="trackingNo">
        <el-input v-model="deliverForm.trackingNo" placeholder="请输入快递单号" class="width-200"></el-input>
      </el-form-item>
    </el-form>

    <!-- 底部按钮 -->
    <div class="dialog-footer mar-t-10">
      <el-button @click="doClose">取消</el-button>
      <el-button type="primary" @click="doConfirm">确定</el-button>
    </div>
  </div>

</template>

<script>
  import { listExpress } from '@/api/goods/express'
  import { doSendDelivery } from '@/api/order/projectSend'

  export default {
    props: {
      showFlag: {
        type: Boolean,
        default() {
          return false
        }
      },
      // 发货单对象
      sendObj: {
        type: Object
      }
    },
    watch: {
      showFlag(val) {
        if (val) {
          this.init()
        }
      }
    },
    data() {
      return {
        // 发货表单
        deliverForm: {
          sendId: '',
          apartOrderId: '',
          enterpriseExpressId: '',
          trackingNo: ''
        },
        // 表单规则
        rules: {
          enterpriseExpressId: [
            { required: true, message: '请选择快递公司', trigger: 'blur' }
          ],
          trackingNo: [
            { required: true, message: '请输入快递单号', trigger: 'blur' }
          ]
        },
        loading: false,
        // 物流集合
        expressList: []
      }
    },
    created() {
      // 获取物流信息
      listExpress({ enterpriseId: this.getEnterpriseId(), supplierId: this.getSupplierId(), warehouseId: this.getWarehouseId(), expressStatus: 1 }).then(response => {
        if (response.isError) return
        this.expressList = response.data
      })
      this.init()
    },
    methods: {
      // 初始化
      init() {
        const originData = this.$options.data()
        this.refreshFlag = originData.refreshFlag
        this.deliverForm = originData.deliverForm
        if (this.$refs['deliverForm']) {
          this.$refs['deliverForm'].clearValidate()
        }
        this.deliverForm.sendId = this.sendObj.sendId
        this.deliverForm.apartOrderId = this.sendObj.apartOrderId
        this.deliverForm.enterpriseExpressId = this.sendObj.enterpriseExpressId
        this.deliverForm.trackingNo = this.sendObj.trackingNo
      },
      // 发货确定事件
      doConfirm() {
        this.$refs['deliverForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            doSendDelivery(this.deliverForm).then(response => {
              this.loading = false
              if (response.isError) return
              this.$message({ type: 'success', message: '发货成功' })
              this.$emit('callback', true)
            })
          }
        })
      },
      // 关闭事件
      doClose() {
        this.$emit('callback', false)
      }
    }
  }
</script>

<style scoped>

</style>
